﻿using System;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataAccess.EnterpriseLibrary.Caching;
using Sevesc.Sps.DomainModel;

namespace Sevesc.Sps
{
    class Program
    {
        static void Main(string[] args)
        {
            DataMapperCollection dataMappers=DomainModel.Mappers.EnterpriseLibrary.Mappers.CreateDataMapperCollection(
                DatabaseFactory.CreateDatabase(),
                new CacheManagerAdapter(CacheFactory.GetCacheManager())
            );


            LieuDeService lds = new LieuDeService(dataMappers);
            lds.Statut = "Conforme";
            lds.Risque = "A risque";

            Contrat c1 = new Contrat(dataMappers);
            c1.Nom = "Contrat 1";
            lds.Contrats.Add(c1);

            Contrat c2 = new Contrat(dataMappers);
            c2.Nom = "Contrat 2";
            lds.Contrats.Add(c2);

            Contrat c3 = new Contrat(dataMappers);
            c3.Nom = "Contrat 3";
            lds.Contrats.Add(c3);

            using (IPersistenceContext context = dataMappers.PersistenceContextProvider.CreatePersistenceContext())
            {
                lds.Save(context);

                context.Commit();
            }

            Console.WriteLine(lds.Contrats.Count + " contrats");

            CacheFactory.GetCacheManager().Flush();

            foreach (LieuDeService lieu in LieuDeService.FindAll(dataMappers))
            {
                Console.WriteLine(lieu.GetId()+"");
                foreach (Contrat contrat in lieu.Contrats)
                    Console.WriteLine("\t" + contrat.GetId() + " : " + contrat.Nom);
            }

            Console.WriteLine("Done.");

            Console.ReadLine();
        }
    }
}
